﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using TMDT.BusinessObjects;

namespace TMDT.BusinessLogic
{
    public class ThuocTinhManager
    {
        public bool Add(ThuocTinh entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spThuocTinhInsert",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDThuocTinh",
                                                                                             entity.IDThuocTinh),
                                                                            new SqlParameter("@TenThuocTinh",
                                                                                             entity.TenThuocTinh)
                                                                        }
                                                    });
        }

        public bool Update(ThuocTinh entity)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spThuocTinhUpdate",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDThuocTinh",
                                                                                             entity.IDThuocTinh),
                                                                            new SqlParameter("@TenThuocTinh",
                                                                                             entity.TenThuocTinh)
                                                                        }
                                                    });
        }

        public bool Remove(object id)
        {
            return BusinessHelper.CapNhatDuLieu(new Command
                                                    {
                                                        CommandText = "spThuocTinhDelete",
                                                        CommandType = CommandType.StoredProcedure,
                                                        Parameter = new[]
                                                                        {
                                                                            new SqlParameter("@IDThuocTinh", id)
                                                                        }
                                                    });
        }

        public List<ThuocTinh> GetThuocTinh()
        {
            List<ThuocTinh> thuocTinh = new List<ThuocTinh>();
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText = "select * from ThuocTinh",
                                                         CommandType = CommandType.Text,
                                                     });
            foreach (DataRow row in dtSet.Tables[0].Rows)
            {
                thuocTinh.Add(ThuocTinh.Convert(row));
            }
            return thuocTinh;
        }

        public ThuocTinh GetThuocTinh(object id)
        {
            DataSet dtSet = new DataSet();
            dtSet = BusinessHelper.TruyVanDuLieu(new Command
                                                     {
                                                         CommandText =
                                                             "select * from ThuocTinh where IDThuocTinh = @IDThuocTinh",
                                                         CommandType = CommandType.Text,
                                                         Parameter = new[]
                                                                         {
                                                                             new SqlParameter("@IDThuocTinh", id),
                                                                         }
                                                     });
            return ThuocTinh.Convert(dtSet.Tables[0].Rows[0]);
        }
    }
}
